<template>
  <view class="chat">
    <!-- 顶部标题 -->
    <view class="topTabbar">
      <!-- 标题 -->
      <view class="text">
        <!-- 返回图标 -->
        <uni-icons class="icon" type="back" size="20" @click="goback()"></uni-icons>
        {{ chatObject }}
      </view>
    </view>
    <scroll-view :style="{ height: `${windowHeight - inputHeight - 180}rpx` }" id="scrollview" scroll-y="true"
                 :scroll-top="scrollTop" class="scroll-view">
      <!-- 聊天主体 -->
      <view id="msglistview" class="chat-body">
        <!-- 聊天记录 -->
        <view v-for="(item, index) in msgList" :key="index">
          <!-- 显示时间 -->
          <view class="showTime">
            {{ timeHandle(item.time) }}
          </view>
          <!-- 自己发的消息 -->
          <view class="item self" v-if="item.userContent != ''">
            <!-- 文字内容 -->
            <view class="content right">
              {{ item.userContent }}
            </view>
            <!-- 头像 -->
            <image class="avatar" :src="item.image"></image>
          </view>
          <!-- 机器人发的消息 -->
          <view class="item Ai" v-if="item.botContent != ''">
            <!-- 头像 -->
            <image class="avatar" :src="item.image">
            </image>
            <!-- 文字内容 -->
            <view class="content left">
              {{ item.botContent }}
            </view>
          </view>
        </view>
      </view>
    </scroll-view>
    <!-- 底部消息发送栏 -->
    <!-- 用来占位，防止聊天消息被发送框遮挡 -->
    <view class="chat-bottom" :style="{ height: `${inputHeight}rpx` }">
      <view class="send-msg" :style="{ bottom: `${keyboardHeight - 60}rpx` }">
        <view class="uni-textarea">
          <textarea v-model="chatMsg" maxlength="300" confirm-type="send" @confirm="handleSend"
                    placeholder="快来发送消息吧～～～"
                    :show-confirm-bar="false" :adjust-position="false" @linechange="sendHeight" @focus="focus"
                    @blur="blur"
                    auto-height></textarea>
        </view>
        <button @click="handleSend" class="send-btn">发送</button>
      </view>
    </view>
  </view>
</template>
<script>
import {extimestampToTime} from '../../utils/handleTime';
import request from "../../utils/request";
import {mapState, mapMutations} from 'vuex'
import {socket} from "../../hook/socket";
import {getUserId} from '../../utils/auth.js';
import dateTime from "../../utils/dateTime.js";
export default {
  websocket: [socket],
  data() {
    return {
      socketOpen: true,
      toId: '',
      chatObject: '',//聊天对象名称
      //键盘高度
      keyboardHeight: 0,
      //底部消息发送高度
      bottomHeight: 0,
      //滚动距离
      scrollTop: 0,
      userId: '',
      //发送的消息
      chatMsg: "",
      socket: null,
      msgList: [
      ],
      userInfo: {},
      receiverid: '',
      messageInfo: {},
      // socket链接，如果在socket.js中改了的话就不用写这个了

    }
  },


async onReady() {
    //初始化值
    await this.getUserInfo()
    // this.webSocketConnect()
    //主页面直接连

    uni.onSocketOpen((res) => {
      console.log('WebSocket 连接已打开！');
    });

    //接收后添加到消息列表
    uni.onSocketMessage((res) => {
      const array = res.data.split(" ")
      const time = array[0] + " " + array[1]
      const fromName = array[2]
      const message = array[3]
      //还需要做一个你判断是否是当前选中的聊天的框用户
      this.msgList.push({
        userContent: "",
        botContent: message,
        image: "/static/images/user/ico.png",
        time: time
      });
    });

    uni.onSocketError((res) => {
      console.log('WebSocket 连接打开失败，请检查！');
    });

    uni.onSocketClose((res) => {
      console.log('WebSocket 连接已关闭！');
    });
  },
  updated() {
    //页面更新时调用聊天消息定位到最底部
    this.scrollToBottom();
  },
  computed: {
    windowHeight() {
      return this.rpxTopx(uni.getSystemInfoSync().windowHeight)
    },
    // 键盘弹起来的高度+发送框高度
    inputHeight() {
      return this.bottomHeight + this.keyboardHeight
    },
  },
  onLoad(optinons) {
    this.receiverid = optinons.receiverid
    this.chatObject = optinons.chatObject
    this.toId = optinons.toId
    // this.updateIsRead()
    uni.onKeyboardHeightChange(res => {
      //这里正常来讲代码直接写
      //this.keyboardHeight=this.rpxTopx(res.height)就行了
      //但是之前界面ui设计聊天框的高度有点高,为了不让键盘和聊天输入框之间距离差太大所以我改动了一下。
      this.keyboardHeight = this.rpxTopx(res.height)
      if (this.keyboardHeight < 0) this.keyboardHeight = 0;
    })
  },
  onUnload() {
    if (this.socket) {
      uni.closeSocket();
    }
  },
  created() {
  },
  destroyed() {

  },
  methods: {

    ...mapMutations('user', ['SET_ISLOGIN', 'SET_USERINFO', 'SET_TOKEN', 'CLEAR_TOKEN']),
	
	timeHandle(e) {
		return dateTime.dateTime1(e) || ''
	},
	

    getUserById() {
      // request({
      //   url: `/userInfo/getUserIdById?userId=${getUserId()}`
      // }).then(res => {
      //   this.userId = res.data
      //   this.getMessagesLists()
      // })
	  this.userId  = getUserId()
	  this.getMessagesLists()
    },

    getMessagesLists() {
      request({
        url: `/messages/getMessages?sendId=${getUserId()}&userId=${this.receiverid}`,
      }).then(res => {
		  console.log(res)
        for (let message of res.data) {
          if (message.senderid === parseInt(this.userId)) {
			
            this.msgList.push({
              userContent: message.messagecontent,
              botContent: "",
              image: this.userInfo.avatarUrl,
              time: message.sendtime?.replace('T', ' ')
            });
          } else {
            this.msgList.push({
              userContent: "",
              botContent: message.messagecontent,
              image: message.avatarUrl ,
              time: message.sendtime?.replace('T', ' ')
            });
          }
        }
      })
    },
    updateIsRead() {
      request({
        url: `/messages/isReadMessages?sendId=${getUserId()}&userId=${this.receiverid}`,
        method: 'POST',
      })
    },
    //获取用户信息
    getUserInfo() {
      request({
        url: '/userInfo/getByToken',
      }).then(res => {
		  console.log(res)
        this.userInfo = res.data
        this.socket = uni.connectSocket({
          url: `ws://localhost:8080/websocket/${this.userInfo.userId}`,
          complete: console.log
        });
		this.getUserById()
      })
    },

    getNickNameById(userId) {
      request({
        url: `/userInfo/getNickNameById/userId=${userId}`,
      }).then(res => {
        console.log(res.data)
      })
    },

    getMessage(id, text) {
      request({
        url: `/SendMessage/sendDirectMessage/${this.userInfo.userId}?id=${id}&text=${text}`,
      }).then(res => {
      })
    },
    goback() {
      uni.navigateBack({})
    },
    focus() {
      this.scrollToBottom()
    },
    blur() {
      this.scrollToBottom()
    },
    // px转换成rpx
    rpxTopx(px) {
      let deviceWidth = uni.getSystemInfoSync().windowWidth
      let rpx = (750 / deviceWidth) * Number(px)
      return Math.floor(rpx)
    },
    // 监视聊天发送栏高度
    sendHeight() {
      setTimeout(() => {
        let query = uni.createSelectorQuery();
        query.select('.send-msg').boundingClientRect()
        query.exec(res => {
          this.bottomHeight = this.rpxTopx(res[0].height)
        })
      }, 10)
    },
    // 滚动至聊天底部
    scrollToBottom(e) {
      setTimeout(() => {
        let query = uni.createSelectorQuery().in(this);
        query.select('#scrollview').boundingClientRect();
        query.select('#msglistview').boundingClientRect();
        query.exec((res) => {
          if (res[1].height > res[0].height) {
            this.scrollTop = this.rpxTopx(res[1].height - res[0].height)
          }
        })
      }, 15)
    },
    // 发送消息
    handleSend() {
      //如果消息不为空
      if (this.chatMsg.trim() !== '') {

        let timeDate = new Date()
        let time = extimestampToTime(timeDate)
        let obj = {
          botContent: "",
          userContent: this.chatMsg,
          image: `${this.userInfo.avatarUrl}`,
          time: time
        }
        this.msgList.push(obj);
        this.scrollToBottom()
        this.sendSocketMessage(this.chatMsg, this.toId)
        this.chatMsg = ''
      } else {
        this.$modal.showToast('不能发送空白消息')
      }
    },

    sendSocketMessage(msg, to) {
      if (this.toId) {
		console.log(this.toId)
        this.getMessage(this.toId, msg)
      } else {
		  console.log(this.receiverid)
        this.getMessage(this.receiverid, msg)
      }
    }
  }

}
</script>
<style lang="scss" scoped>
$chatContentbgc: #C2DCFF;
$sendBtnbgc: #4F7DF5;

view,
button,
text,
input,
textarea {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* 聊天消息 */
.chat {
  .topTabbar {
    width: 100%;
    height: 90rpx;
    line-height: 90rpx;
    display: flex;
    margin-top: 80rpx;
    justify-content: space-between;

    .text {
      position: relative;
      width: 100%;
      font-size: 16px;
      font-weight: 700;
      text-align: center;

      .icon {
        position: absolute;
        left: 10px;
      }

    }

    .button {
      width: 10%;
      margin: auto 20rpx auto 0rpx;
    }
  }

  .scroll-view {
    ::-webkit-scrollbar {
      display: none;
      width: 0 !important;
      height: 0 !important;
      -webkit-appearance: none;
      background: transparent;
      color: transparent;
    }

    // background-color: orange;
    background-color: #F6F6F6;

    .chat-body {
      display: flex;
      flex-direction: column;
      padding-top: 23rpx;
      // background-color:skyblue;

      .showTime {
        width: 100%;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 10px;
      }

      .self {
        justify-content: flex-end;
      }

      .item {
        display: flex;
        padding: 23rpx 30rpx;
        // background-color: greenyellow;

        .right {
          background-color: $chatContentbgc;
        }

        .left {
          background-color: #FFFFFF;
        }

        // 聊天消息的三角形
        .right::after {
          position: absolute;
          display: inline-block;
          content: '';
          width: 0;
          height: 0;
          left: 100%;
          top: 10px;
          border: 12rpx solid transparent;
          border-left: 12rpx solid $chatContentbgc;
        }

        .left::after {
          position: absolute;
          display: inline-block;
          content: '';
          width: 0;
          height: 0;
          top: 10px;
          right: 100%;
          border: 12rpx solid transparent;
          border-right: 12rpx solid #FFFFFF;
        }

        .content {
          position: relative;
          max-width: 486rpx;
          border-radius: 8rpx;
          word-wrap: break-word;
          padding: 24rpx 24rpx;
          margin: 0 24rpx;
          border-radius: 5px;
          font-size: 32rpx;
          font-family: PingFang SC;
          font-weight: 500;
          color: #333333;
          line-height: 42rpx;
        }


        .avatar {
          display: flex;
          justify-content: center;
          width: 78rpx;
          height: 78rpx;
          background: $sendBtnbgc;
          border-radius: 50rpx;
          overflow: hidden;

          image {
            align-self: center;
          }

        }
      }
    }
  }

  /* 底部聊天发送栏 */
  .chat-bottom {
    width: 100%;
    height: 100rpx;
    background: #F4F5F7;
    transition: all 0.1s ease;

    .send-msg {
      display: flex;
      align-items: flex-end;
      padding: 16rpx 30rpx;
      width: 100%;
      min-height: 177rpx;
      position: fixed;
      bottom: 0;
      background: #fff;
      transition: all 0.1s ease;
    }

    .uni-textarea {
      padding-bottom: 70rpx;

      textarea {
        width: 537rpx;
        min-height: 75rpx;
        max-height: 500rpx;
        background: #f1f1f1;
        border-radius: 40rpx;
        font-size: 32rpx;
        font-family: PingFang SC;
        color: #333333;
        line-height: 74rpx;
        padding: 20rpx 8rpx;
        text-indent: 30rpx;
      }
    }

    .send-btn {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 76rpx;
      margin-left: 25rpx;
      width: 120rpx;
      height: 75rpx;
      background: #ed5a65;
      border-radius: 50rpx;
      font-size: 28rpx;
      font-family: PingFang SC;
      font-weight: 500;
      color: #FFFFFF;
      line-height: 28rpx;
    }
  }
}
</style>
